package com.example.demo.controller;

import org.json.JSONObject;

public class p {
    public static String zhixing(String method) {
        String res = "";
        if (method.equals("jilushichang")) {
            res = jilushichang();
        }
        if (method.equals("getxuanzilist")) {
            res = getxuanzilist();
        }
        if (method.equals("getceshi")) {
            res = getceshi();
        }
        if (method.equals("jiesuo")) {
            res = jiesuo();
        }
        if (method.equals("getdaka")) {
            res = getdaka();
        }
        if (method.equals("getshichang")) {
            res = getshichang();
        }
        if (method.equals("paiming")) {
            res = paiming();
        }
        if (method.equals("getfenshu")) {
            res = getfenshu();
        }
        return res;
    }

    static String jilushichang() {
        String uid = manifest.jsonObject.opt("uid").toString();
        String day = manifest.jsonObject.opt("day").toString();
        String sql = String.format("""
                    UPDATE user set shichang = shichang + 1 where id = %s
                """, uid);
        manifest.caozuo(sql);
        int num = manifest.chaxun_num(String.format("""
                    SELECT * FROM jilu where uid = %s and DATE(addtime) = '%s'
                """, uid, day));
        String sql2 = "";
        if (num == 0) {
            sql2 = String.format("""
                        INSERT into jilu (uid,shichang) VALUES ('%s',1)
                    """, uid);
            return manifest.caozuo(sql2);
        } else {
            String id = gongju.ogetstr(manifest.chaxun_one_2(String.format("""
                        SELECT id from jilu where uid = %s and DATE(addtime) = '%s'
                    """, uid, day)), "id");
            return manifest.caozuo(String.format("""
                        UPDATE jilu set shichang = shichang + 1 where id = '%s'
                    """, id));
        }

    }

    static String getxuanzilist() {
        String nandu = manifest.jsonObject.opt("nandu").toString();
        String zi = manifest.jsonObject.opt("zi").toString();
        String sql = String.format("""
                    SELECT * FROM wenzi where nandu = '%s' and zi != '%s' order by rand() limit 3
                """, nandu, zi);
        return manifest.chaxun(sql);
    }

    static String getceshi() {
        String nandu = manifest.jsonObject.opt("nandu").toString();
        String sql = String.format("""
                    SELECT * FROM lianxi where nandu = '%s' order by rand() limit 10
                """, nandu);
        return manifest.chaxun(sql);
    }

    static String jiesuo() {
        String uid = manifest.jsonObject.opt("uid").toString();
        String sql = String.format("""
                    UPDATE user set nandu = nandu + 1 where id = %s
                """, uid);
        return manifest.caozuo(sql);
    }

    static String getdaka() {
        String uid = manifest.jsonObject.opt("uid").toString();
        String day = manifest.jsonObject.opt("day").toString();
        String sql = String.format("""
                    SELECT DATE(addtime) as day FROM jilu where uid = %s and DATE(addtime) in (%s) and shichang >= 5
                """, uid, day);
        return manifest.chaxun(sql);
    }

    static String getshichang() {
        String uid = manifest.jsonObject.opt("uid").toString();
        String day = manifest.jsonObject.opt("day").toString();
        String sql = String.format("""
                    SELECT shichang FROM jilu where uid = %s and DATE(addtime) = '%s' limit 1
                """, uid, day);
        return manifest.chaxun_one(sql);
    }

    static String paiming() {
        String sql = """
                    SELECT * FROM user order by shichang desc limit 10
                """;
        return manifest.chaxun(sql);
    }

    static String getfenshu() {
        JSONObject j = new JSONObject();
        Object[] n1 = manifest.chaxun_2("SELECT avg(fenshu) as jf,uid FROM ceshi where nandu = '入门' group by uid");
        Object[] n2 = manifest.chaxun_2("SELECT avg(fenshu) as jf,uid FROM ceshi where nandu = '低' group by uid");
        Object[] n3 = manifest.chaxun_2("SELECT avg(fenshu) as jf,uid FROM ceshi where nandu = '中' group by uid");
        Object[] n4 = manifest.chaxun_2("SELECT avg(fenshu) as jf,uid FROM ceshi where nandu = '高' group by uid");
        j.put("code", 200);
        j.put("n1", n1);
        j.put("n2", n2);
        j.put("n3", n3);
        j.put("n4", n4);
        return j.toString();
    }
}
